<!--
  * 商品明细
  *
-->
<template>
  <a-table :dataSource="props.orderList" :columns="columns" :pagination="false" rowKey="id" size="small" bordered :scroll="{ x: 'max-content' }" :rowSelection="{ selectedRowKeys: selectedKeys, onChange: onSelectedChange, getCheckboxProps: getSelectedCheckBoxProps }">
    <template #bodyCell="{ text, record, column }">
      <template v-if="column.dataIndex === 'no'">
        <CopyText :text="text">
          <a @click="changeGoodsModal(true, record)">{{ text }}</a>
        </CopyText>
      </template>
      <template v-if="column.dataIndex === 'order_type'">
        <span>{{ getDescByValue('ORDER_STATRS_ENUM', text) }}</span>
      </template>
      <template v-if="column.dataIndex === 'status'">
        <span>{{ getDescByValue('PROGRESS_STATUS_ENUM', text) }}</span>
      </template>
      <template v-if="column.dataIndex === 'warehouseName'">
        <span> {{ record?.warehouse?.name }}({{ record?.warehouse?.warehouse_attribute_name }})</span>
      </template>
      <template v-if="column.dataIndex === 'action'">
        <div class="smart-table-operate">
          <a-button @click="changeGoodsModal(true, record)" type="link" style="color: #97999c">详情</a-button>
          <!-- <template v-if="props.row.status === DELIVERY_STATUS_ENUM.DELIVERY.value">
            <a-button @click="onModifyOrder(record)" type="link">编辑</a-button>
          </template>
          <template v-if="props.row.status === DELIVERY_STATUS_ENUM.DELIVERY.value">
            <a-button @click="cancelOrder(record)" danger type="link" v-privilege="'delivery:scheduling'">驳回</a-button>
          </template> -->
          <!-- <a-button @click="deleteData(record.id, record.no)" danger type="link" v-privilege="'goods:delete'">删除</a-button> -->
        </div>
      </template>
    </template>
  </a-table>
  <a-modal v-model:open="showGoodsModal" title="商品信息" :footer="null" width="50vw">
    <a-table :dataSource="currentOrderGoodsData" :columns="orderGoodsColumns" :scroll="{ x: 'max-content' }" :pagination="false" rowKey="id" size="small" bordered>
      <template #bodyCell="{ text, record, index, column }">
        <template v-if="column.dataIndex === 'num'"> {{ text }}{{ record.spec.unit_name }} </template>
      </template>
    </a-table>
  </a-modal>
</template>
<script setup lang="ts">
import _ from 'lodash'
import { useEnums } from '@/hooks/plugins/useEnums'
import { ORDER_STATRS_ENUM } from '@/constants/business/orders-const'
import { DELIVERY_STATUS_ENUM } from '@/constants/business/storage-const'
import { taskApi } from '@/api/business/logistics/delivery-api'
import { message, Modal } from 'ant-design-vue'
import { errorSentry } from '@/lib/error-sentry'
import { useLoadingStore } from '@/store/modules/loading-store'
const { getDescByValue } = useEnums()
const loadingStore = useLoadingStore()
const props = defineProps({
  row: {
    type: Object,
    default: () => ({})
  },
  orderList: {
    type: Array,
    default: () => []
  }
})
const emit = defineEmits(['onSelectChange', 'reloadList'])
const columns = reactive([
  {
    title: '单据编号',
    dataIndex: 'no'
  },
  {
    title: '门店',
    dataIndex: ['store', 'store_name']
  },
  {
    title: '订单类型',
    dataIndex: 'order_type'
  },
  {
    title: '订单状态',
    dataIndex: 'status'
  },
  {
    title: '业务员',
    dataIndex: ['salesman', 'rename']
  },
  {
    title: '开单仓库',
    dataIndex: 'warehouseName'
  },
  {
    title: '经销商',
    dataIndex: ['dealer', 'name']
  },
  {
    title: '备注',
    dataIndex: 'remark'
  },
  {
    title: '日期',
    dataIndex: 'create_time'
  },

  {
    title: '操作',
    dataIndex: 'action',
    fixed: 'right'
  }
])
const selectedKeys = ref([])
function onSelectedChange(keys, selectedRows) {
  selectedKeys.value = keys
  emit('onSelectChange', selectedRows)
}
function getSelectedCheckBoxProps(record) {
  return {
    disabled: [ORDER_STATRS_ENUM.ACCOUNT.value, ORDER_STATRS_ENUM.REFUND.value].includes(record.order_type)
  }
}
function clearSelectedKeys(keys?: any[]) {
  if (keys?.length && keys?.length > 0) {
    const list = new Set(keys)
    const selectedRows = props.orderList.filter((item) => !list.has(item.id))
    selectedKeys.value = selectedKeys.value.filter((item) => !list.has(item))
    emit('onSelectChange', selectedRows)
  } else {
    selectedKeys.value = []
    emit('onSelectChange', [])
  }
}
defineExpose({
  clearSelectedKeys
})

const currentOrderGoodsData = ref<any[]>([])
// ---------------------------- 订单商品明细 ----------------------------
const showGoodsModal = ref(false)
const orderGoodsColumns = reactive([
  {
    title: '商品名称',
    dataIndex: ['goods', 'goods_name']
  },
  {
    title: '规格',
    dataIndex: ['spec', 'spec_name']
  },
  {
    title: '数量',
    dataIndex: 'num'
  },
  {
    title: '单价',
    dataIndex: 'real_price'
  },
  {
    title: '小计',
    dataIndex: 'total_price'
  },
  {
    title: '备注',
    dataIndex: 'remark'
  }
])
function changeGoodsModal(flag: boolean, rowData?: any) {
  showGoodsModal.value = flag
  rowData && (currentOrderGoodsData.value = rowData.detail)
}

// ----------------------------- 编辑销售单 -----------------------------
const { push } = useRouter()
function onModifyOrder(rowData?: any) {
  // const path = rowData?.order_type === 1 ? '/sale/sales' : '/sale/return'
  const path = rowData?.order_type === 1 ? '/sale/sales' : rowData?.order_type === 3 ? '/sale/barter' : '/sale/return'
  push({ path, query: { id: rowData.id } })
}
// ---------------------------- 取消 ----------------------------

function cancelOrder(goodsData) {
  Modal.confirm({
    title: '提示',
    content: '确定驳回吗?',
    okText: '确定',
    onOk() {
      singleDelete(goodsData)
    },
    cancelText: '取消',
    onCancel() {}
  })
}

async function singleDelete(goodsData) {
  try {
    loadingStore.show()
    const res = await taskApi.cancelScheduling({ id: props.row.id, order_id: goodsData.id })
    message.success(res.msg)
    emit('reloadList')
  } catch (e) {
    message.error(e.msg)
    errorSentry.captureError(e)
  } finally {
    loadingStore.hide()
  }
}
</script>
